<?php
/**
 * Created by PhpStorm.
 * User: HarrisHuang
 * Date: 17/5/21
 * Time: 20:13
 */
namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Models\Statistics;
use App\Models\JumpLog;
use Log;
use Illuminate\Support\Facades\Redis;

class StatisticsController extends Controller
{

    /**
     * 添加访问记录
     */
    public function save(Request $request, Statistics $statistics)
    {
        $type = (int)$request->input('type');
        $pid = $request->input('pid');
        $pName = $request->input('pName');
        $uid = $request->input('uid');
        $date = date('Ymd');
        $info = $statistics->where('type',$type)
            ->where('pid', $pid)
            ->where('date', $date)
            ->first();
        if($info){
            //修改
            if($this->getUv($date, $type, $pid, $uid))  $info->uv = $info['uv'] + 1;
            $info->pv = $info['pv'] + 1;
            $info->save();
        }else{
            $info = new Statistics();
            $info->type = $type;
            $info->pid = $pid;
            $info->pName = $pName;
            $info->date = $date;
            $info->pv = 1;
            $info->uv = 1;
            $info->save();
        }
        return $this->success();
    }

    /**
     * 跳转第三方网站统计
     * 立即申请操作
     */
    public function addThirdJump(Request $request, Statistics $statistics, JumpLog $jumpLog)
    {
        $type = (int)$request->input('type');
        $pid = $request->input('pid');
        $pName = $request->input('pName');
        $uid = $request->input('uid');
        $date = date('Ymd');
        $info = $statistics->where('type',$type)
            ->where('pid', $pid)
            ->where('date', $date)
            ->first();
        if($info){
            if($this->getJumpNum($date, $type, $pid, $uid))  $info->jumpNum = $info['jumpNum'] + 1;
            $info->jumpTimes = $info['jumpTimes'] + 1;
            $info->save();
        }else{
            $info->type = $type;
            $info->pid = $pid;
            $info->pName = $pName;
            $info->date = $date;
            $info->jumpTimes = 1;
            $info->jumpNum = 1;
            $info->save();
        }
        //添加跳转记录
        $jumpLog->pid = $pid;
        $jumpLog->uid = $uid;
        $jumpLog->pName = $pName;
        $jumpLog->addTime = time();
        $jumpLog->save();
        return $this->success();
    }

    public function lists(Request $request)
    {

        $type = (int)$request->get('type');
        $pageSize = $request->get('pageSize', 10);
        $startTime = $request->get('startTime',0);
        $endTime = $request->get('endTime',0);
        $keyWords = $request->get('keyWords','');
        if($type <=0 ){
            return $this->error();
        }
        $list = Statistics::where('type',$type);
        if($startTime > 0){
            $list = $list->where('date','>=', $startTime);
        }
        if($endTime > 0){
            $list = $list->where('date','<=', $endTime);
        }
        if($keyWords != ''){
            $list = $list->where('pName','like', '%'.$keyWords.'%');
        }
        $list = $list->paginate($pageSize);
        $list = $list->toArray();
        $res = [
            'total' => $list['total'],
            'data' => $list['data'],
        ];
        return $this->success($res);

    }

    /**
     * 判断计算uv,需要去重
     */
    public function getUv($date, $type, $pid, $uid)
    {
        $key = 'ymd.statistics.product.uv.'.$date;
        if(Redis::hGet($key,$type.'-'.$pid.'-'.$uid)){
           return false;
        }
        Redis::hSet($key, $type.'-'.$pid.'-'.$uid, 1);
        Redis::expire($key, 86400);
        return true;
    }

    /**
     * 计算跳转到第三方人数,需要去重
     */
    public function getJumpNum($date, $type, $pid, $uid)
    {
        $key = 'ymd.statistics.product.jump.third.'.$date;
        if(Redis::hGet($key,$type.'-'.$pid.'-'.$uid)){
            return false;
        }
        Redis::hSet($key, $type.'-'.$pid.'-'.$uid, 1);
        Redis::expire($key, 86400);
        return true;
    }

}